<?php
/*
	服务公司：沧州讯呗网络科技有限公司
	作者：sun
*/
namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\AddressRepository;
use App\Repositories\DeliveryRepository;
use App\Traits\BaseFun;

class AddressController extends Controller
{
    public function __construct(AddressRepository $AddressRepository,DeliveryRepository $DeliveryRepository)
    {
        $this->AddressRepository=$AddressRepository;
        $this->DeliveryRepository=$DeliveryRepository;
    }
    public function Address(Request $request)
    {   
       
        $res = $this->AddressRepository->list(['userid'=>$request->userId]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function AddressAdd(Request $request)
    {
        $res = $this->AddressRepository->create($request->all());
        $resList = $this->AddressRepository->list(['userid'=>$request->userId]);
        if ($request->state==1 && count($resList)) {
            foreach ($resList as $key => $value) {
                if ($value['id'] != $res) {
                    $this->AddressRepository->update(['id'=>$value['id'],'state'=>2]);
                }
                
            }
        }
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '添加失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function AddressEdit(Request $request,$id)
    {
        $res = $this->AddressRepository->info(['id'=>$id]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '查询数据失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function AddressUpdate(Request $request)
    {
        $res = $this->AddressRepository->update($request->all());
        $resList = $this->AddressRepository->list(['userid'=>$request->userId]);
        if ($request->state==1 && count($resList)) {
            foreach ($resList as $key => $value) {
                if ($value['id'] != $request->id) {
                    $this->AddressRepository->update(['id'=>$value['id'],'state'=>2]);
                }
                
            }
        }
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '修改失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function AddressDel(Request $request,$id)
    {
        $res = $this->AddressRepository->delete($id);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '删除失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function RestaurantAddress(Request $request)
    {
        $res = $this->DeliveryRepository->info(['id'=>1]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '删除失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }


}
